package docker

import "time"

// 服务配置
type ServiceConfig struct {
	// 基本信息
	Name     string
	Image    string
	Replicas uint64
	Port     uint32

	// 资源限制
	CPULimit      float64
	CPURequest    float64
	MemoryLimit   int64
	MemoryRequest int64

	// 环境变量
	Env map[string]string

	// 健康检查
	HealthCheck bool
	HealthPath  string
}

// 服务状态
type ServiceStatus struct {
	// 任务状态
	RunningTasks  uint64
	DesiredTasks  uint64
	FailedTasks   uint64
	UpdateStatus  string
	LastError     string
	LastUpdated   time.Time
	LastHealthy   time.Time
	LastUnhealthy time.Time
}

// 默认配置
const (
	// 日志配置
	DefaultLogDriver = "json-file"

	// 更新配置
	DefaultUpdateParallelism   uint64        = 1
	DefaultUpdateDelay         time.Duration = 10 * time.Second
	DefaultUpdateFailureAction = "rollback"
	DefaultUpdateOrder        = "start-first"

	// 健康检查配置
	HealthCheckInterval      = 5 * time.Second
	HealthCheckTimeout       = 2 * time.Second
	HealthCheckRetries      = 3
	HealthCheckTotalTimeout = 5 * time.Minute

	// 监控配置
	MonitorInterval = 5 * time.Second
)

// 默认日志选项
var DefaultLogOpts = map[string]string{
	"max-size": "10m",
	"max-file": "3",
}

// 服务状态常量
const (
	ServiceStatusRunning = "running"
	ServiceStatusFailed  = "failed"
	ServiceStatusPending = "pending"
)

// 更新状态常量
const (
	UpdateStatusCompleted = "completed"
	UpdateStatusPaused    = "paused"
	UpdateStatusRollingBack = "rolling-back"
	UpdateStatusFailed     = "failed"
)

// 健康状态常量
const (
	HealthStatusHealthy   = "healthy"
	HealthStatusUnhealthy = "unhealthy"
	HealthStatusStarting  = "starting"
)

// 资源限制常量
const (
	DefaultCPULimit      = 1.0    // 1 CPU
	DefaultCPURequest    = 0.5    // 0.5 CPU
	DefaultMemoryLimit   = 2048   // 2GB
	DefaultMemoryRequest = 1024   // 1GB
	BytesPerMB          = 1024 * 1024
	BytesPerGB          = BytesPerMB * 1024
)

// 端口范围常量
const (
	MinPort = 1
	MaxPort = 65535
)

// 副本数限制
const (
	MinReplicas = 1
	MaxReplicas = 10
)

// 错误重试配置
const (
	MaxRetries    = 3
	RetryInterval = 5 * time.Second
	RetryBackoff  = 2.0
)

// 监控配置
const (
	MonitorBufferSize = 100
	MonitorTimeout    = 30 * time.Second
)

// 服务标签
const (
	LabelServiceName = "com.docker.swarm.service.name"
	LabelEnvironment = "com.docker.swarm.service.env"
	LabelVersion     = "com.docker.swarm.service.version"
)

// 网络配置
const (
	DefaultNetwork = "overlay"
	DefaultDriver  = "overlay"
)

// 容器配置
const (
	DefaultStopGracePeriod = 30 * time.Second
	DefaultRestartDelay    = 5 * time.Second
	MaxRestartAttempts     = 3
)

// JVM配置
const (
	DefaultJavaOpts = "-XX:+UseContainerSupport -XX:MaxRAMPercentage=75.0"
)

// 环境变量
var DefaultEnvVars = map[string]string{
	"JAVA_TOOL_OPTIONS": DefaultJavaOpts,
	"TZ":               "Asia/Shanghai",
}

// 资源单位转换
func MB(mb int64) int64 {
	return mb * BytesPerMB
}

func GB(gb int64) int64 {
	return gb * BytesPerGB
}

// 合并环境变量
func MergeEnvVars(envs ...map[string]string) map[string]string {
	result := make(map[string]string)

	// 添加默认环境变量
	for k, v := range DefaultEnvVars {
		result[k] = v
	}

	// 合并其他环境变量
	for _, env := range envs {
		for k, v := range env {
			result[k] = v
		}
	}

	return result
}
